<?php

namespace Pinenut\Files;

use Pinenut\Action\JSON;

/**
 * Загружаем фотку пользователя
 * идентификатор пользователя сохраняем
 * 
 * @author kosh
 *
 */
abstract class UploadAction extends JSON {
	protected static $isPublic = true;
	
	protected static $_modelClass;
	
	protected $additionalParams = [];
	
	static function pullParams() {
		return ['isPost'];
	}
	
	protected function run() {
		
		$result = [ ];
		if ($this->request->_isPost) {
			$this->response->noCache(true);
			foreach ( $_FILES ['file']['tmp_name'] as $filename ) {
				$mimetype = exec ( 'file -i -b ' . escapeshellarg ( $filename ) );
				if (strpos ( $mimetype, ';' )) {
					list ( $mimetype, $charset ) = explode ( ';', $mimetype );
				}
				if (! preg_match ( '/^image/i', $mimetype )) {
					continue;
				}
				$class = static::$_modelClass;
				$file = $class::create ( $filename, $this->additionalParams);
				
				$result[] = (string)$file;
			}
		}
		return $result;
	}
}
